'''
Author: Yaodecheng
Date: 2020-12-04 21:39:57
LastEditTime : 2020-12-05 09:42:30
LastEditors  : YaoDecheng
'''
import csv
from tkinter import Tk, filedialog
from os import path


def choice_file():
    root = Tk()
    root.withdraw()
    default_dir = path.abspath('./')

    Folderpath = filedialog.askopenfilename(title=u'待转换文件', filetypes=[(
        'CSV', '*.csv'), ('All Files', '*')], initialdir=(path.expanduser(default_dir)))
    return Folderpath


if __name__ == "__main__":
    f_path=choice_file()
    if(f_path):
        new_name = f_path.replace('.csv', '_to_jlc.csv')
        
        with open(f_path)as f:
            f_csv = csv.reader(f)
            headers = ['Designator', 'Footprint', 'Mid X',
                    'Mid Y', 'Layer', 'Rotation', 'Comment']
            with open(new_name, 'w', newline='')as fw:
                fw_csv = csv.writer(fw)
                fw_csv.writerow(headers)
                index = 0
                for row in f_csv:
                    if(index == 0):
                        index = index+1
                        continue
                    fw_csv.writerow([row[0],row[2],row[3],row[4],row[6][0].upper(),row[5],row[1]])
            f.close()
            fw.close()